package cn.nawang.ebeim.client.version;

import com.alibaba.fastjson.annotation.JSONField;

import java.io.File;
import java.util.List;

public class FileNode implements Node {

    private static final long serialVersionUID = 1L;

    private String name;

    private String dsId;

    private Node parent;

    private byte status = Node.STATUS_NOOP;

    private String path;

    private String signature;

    private long fileSize;

    private transient boolean isNewNode;

    private transient boolean hasRefresh;

    private long id;

    private long pId;

    public FileNode() {
    }

    public FileNode(String name) {
        this.name = name;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JSONField(serialize = false)
    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setPId(long pId) {
        this.pId = pId;
    }

    public long getPId() {
        return pId;
    }

    public byte getStatus() {
        return status;
    }

    public void setStatus(byte status) {
        this.status = status;
    }

    public String getPath() {
        return path;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public void setDsId(String dsId) {
        this.dsId = dsId;
    }

    public String getDsId() {
        return dsId;
    }

    public void setNewNode(boolean newNode) {
        this.isNewNode = newNode;
    }

    public boolean filterChanges(List<Node> list) {
        if (status != STATUS_NO_CHANGE) {
            list.add(this);
            return true;
        }
        return false;
    }

    public VersionTreeInfo versionTreeInfo(VersionTreeInfo versionTreeInfo) {
        if (status != STATUS_DELETED && status != STATUS_DELETED) {
            versionTreeInfo.fileNodes.add(this);
            versionTreeInfo.totalBytes += this.fileSize;
        }
        return versionTreeInfo;
    }

    public void refresh() {
        if (hasRefresh || isNewNode) {
            return;
        }
        hasRefresh = true;
        File file = new File(path);
        if (!file.exists()) {
            this.status = STATUS_DELETED;
        } else {
//            String md5Hex = FileUtil.md5Hex(path);
            if (fileSize==file.length()) {
                this.status = STATUS_NO_CHANGE;
            } else {
                this.status = STATUS_CHANGE;
//                this.signature = md5Hex;
                this.fileSize = file.length();
            }
        }
    }

    @Override
    public String toString() {
        return "FileNode{" +
                "name='" + name + '\'' +
                ", status=" + status +
                ", path='" + path + '\'' +
                ", signature='" + signature + '\'' +
                ", fileSize=" + fileSize +
                ", id=" + id +
                '}';
    }
}
